<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label class="layui-form-label">结账状态</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.CHDSTATUSEuqals" placeholder="请选择结账状态" @change="btnSearch">
            <el-option label="全部" :value="null"></el-option>
            <el-option v-for="item in options" :key="item.Value" :label="item.Text" :value="item.Value" />
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label>处理日期从</label>
        <el-date-picker v-date v-model="selectData.ChddateGreaterAndEquals" type="datetime" placeholder="输入日期"
          format="yyyy-MM-dd HH:mm" @change="btnSearch"></el-date-picker>
        到
        <el-date-picker v-date v-model="selectData.ChddateLessAndEquals" type="datetime" placeholder="输入日期"
          format="yyyy-MM-dd HH:mm" @change="btnSearch"></el-date-picker>
      </div>
      <el-button type="primary" @click="btnSearch" :disabled="downloadLoading" :loading="this.$store.state.isloading">搜索
      </el-button>
      <el-button :disabled="$store.state.isloading || downloadLoading" @click="btnRepeat">重置</el-button>
    </div>
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon"
          @click="operate(item.BTNVALUE)">
          <el-button type="primary" :disabled="$store.state.isloading || downloadLoading" :value="item.BTNSHOWNAME"
            v-text="item.BTNSHOWNAME"></el-button>
        </li>
        <li>
          <el-button type="primary" :disabled="this.$store.state.isloading" @click="exportExcel">导出Excel</el-button>
        </li>
      </ul>
    </div>
    <printmodal ref="printPdf"></printmodal>
    <dataTable :headList="headList"  :isIndex="true" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange"
      @selectRow="selectRow" :hiddenPageOrder="false" :height="height"></dataTable>
    <h1 style="text-align:right;margin-right:100px;color:red;font-size:20px;">{{message}}</h1>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";

  .el-button+.el-button {
    margin-left: 0;
  }

  .btn {
    text-align: center;
  }

</style>
<script>
  import dataTable from "@/components/Table/DataTable";
  import printmodal from "@/components/Pdf/Print";
  import apiBase from "@/api/apiBase.js";
  import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  import {
    ORDER
  } from "@/const/orderConst.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  import {
    PRINT
  } from "@/components/Pdf/PDF.js";
  import sweetModal from "@/components/sweetModal";

  const EXPORT = "打印";
  const INFO = "详情";
  export default {
    name: "saleStatistics",
    mixins: [manage],
    data() {
      return {
        options: [], //结账状态
        selectData: {
          ChddateGreaterAndEquals: null,
          ChddateLessAndEquals: null,
          CHDSTATUSEuqals: null
        },
        IsGetGeneralByMoney: null,
        downloadLoading: false,
        OutDataExcel: [],
        title: null,
        order: null,
        message: null,
        height: "620",
        //分页和排序
        pageOrderInfo: {
          orderType: null,
          orderSort: null,
          totalNum: null,
          pageSize: 10,
          currentPage: 1
        },
        sweetModalWidth: 80,
        sweetModalMaxWidth: 1000,

        chaTypes: [],
        chaTypeList: []
      };
    },
    methods: {
      exportExcel() {
        var header;
        var startTime;
        if (this.selectData.ChddateGreaterAndEquals) {
          startTime = this.selectData.ChddateGreaterAndEquals.toLocaleDateString();
        } else {
          startTime = "空";
        }
        if (this.selectData.ChddateLessAndEquals != null) {
          var endTime = this.selectData.ChddateLessAndEquals.toLocaleDateString();
          header =
            localStorage.getItem("orgname") +"流水账汇总表" +"(" + startTime +"--" +endTime + ")";
        } else {
          header =localStorage.getItem("orgname") + "流水账汇总表" + "(" + startTime +"--" +"至今" +")";
        }
        if (this.dataList.length === 0) {
          this.$message.error("无导出数据！");
          return;
        }
        this.downloadLoading = true;
        require.ensure([], () => {
          const {
            export_json_to_excel
          } = require("@/vendor/Export2Excel");
          export_json_to_excel(this.headList, this.OutDataExcel, header);
          this.downloadLoading = false;
        });
      },
      //功能操作
      operate(order) {
        this.order = order;
        this.dataRepeat();
        switch (this.order) {
          case "打印":
            let Data = JSON.parse(JSON.stringify(this.selectData));
            this.$refs.printPdf.report(PRINT.PDFRUNNING, this.getDataC());
            break;
          default:
        }
      },
      getColumns() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Column.GetRunningAccountSummaryColumns,
          res => {
            this.headList = res.data.content;
          }
        );
      },
      getDataC() {
        let Data = JSON.parse(JSON.stringify(this.selectData));
        Data.ChddateGreaterAndEquals = formatDate(
          Data.ChddateGreaterAndEquals,
          "yyyyMMddhhmm"
        );
        if (Data.ChddateGreaterAndEquals != null) {
          Data.ChddateGreaterAndEquals =
            Data.ChddateGreaterAndEquals + "00";
        }
        Data.ChddateLessAndEquals = formatDate(
          Data.ChddateLessAndEquals,
          "yyyyMMddhhmm"
        );
        if (Data.ChddateLessAndEquals != null) {
          Data.ChddateLessAndEquals = Data.ChddateLessAndEquals + "59";
        }
        let data = {
          PageIndex: 1,
          PageSize: 200,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        return data;
      },
      getData() {
        this.getColumns();
        let Data = JSON.parse(JSON.stringify(this.selectData));
        Data.ChddateGreaterAndEquals = formatDate(
          Data.ChddateGreaterAndEquals,
          "yyyyMMddhhmm"
        );
        if (Data.ChddateGreaterAndEquals != null) {
          Data.ChddateGreaterAndEquals =
            Data.ChddateGreaterAndEquals + "00";
        }
        Data.ChddateLessAndEquals = formatDate(
          Data.ChddateLessAndEquals,
          "yyyyMMddhhmm"
        );
        if (Data.ChddateLessAndEquals != null) {
          Data.ChddateLessAndEquals = Data.ChddateLessAndEquals + "59";
        }
        let data = {
          PageIndex: 1,
          PageSize: 200,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Report.QueryRrunningAccountRecordSummary,
          res => {
            this.OutDataExcel = res.data.content.OutDataExcel;
            this.dataList = res.data.content.OutData;
            this.message = res.data.content.Message;
            this.pageOrder.totalNum = res.data.content.RecordCount;
          }
        );
      },
      //查询条件重置
      btnRepeat() {
        this.selectData = {
          ChddateGreaterAndEquals: null,
          ChddateLessAndEquals: null
        };
        this.selectData.ChddateGreaterAndEquals = new Date(
          new Date().toLocaleDateString()
        );
        this.selectData.ChddateLessAndEquals = new Date(
          new Date(new Date().toLocaleDateString()).getTime() +
          24 * 60 * 60 * 1000 -
          1
        );
        this.getData();
      },
      getGetChdStatus() {
        apiBase.DoService({},
          apiBase.getApiAdress().Checkout.GetChdStatus,
          res => {
            this.options = res.data.content;
          }
        );
      },
      //分页和排序
      pageChangeInfo(value) {
        this.pageOrderInfo = value;
      },
      btnShow(btn) {
        switch (btn) {
          case EXPORT:
            return true;
            break;
          default:
            return false;
        }
      },
      //数据重置
      dataRepeat() {}
    },
    mounted() {},
    components: {
      dataTable,
      sweetModal,
      printmodal
    },
    //当前选中行
    selectRow(row) {
      this.currentRow = row;
      this.$emit("selectRow", row, "");
    },
    beforeCreate() {},
    created() {
      this.selectData.ChddateGreaterAndEquals = new Date(
        new Date().toLocaleDateString()
      );
      this.selectData.ChddateLessAndEquals = new Date(
        new Date(new Date().toLocaleDateString()).getTime() +
        24 * 60 * 60 * 1000 - 1
      );
      this.getGetChdStatus(); //获取结账下拉
      this.getColumns();
      this.getData(); //获取数据
    }
  };

</script>
